package so.shanku.essential.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import me.kaede.tagview.OnTagClickListener;
import me.kaede.tagview.TagView;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseActivity;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.IGetServicesDataCallBack;
import so.shanku.essential.getdata.JsonKeys;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.StringUtil;
import so.shanku.essential.utils.Utils;

/**
 * Created by Jaelyn on 2015/9/19 0019.
 * 选择商品的参数
 */
public class SelectGoodsParamsActivity extends BaseActivity {
//	@ViewInject(id = R.id.expendlist)
//	private ExpandableListView goods_expandLv;

    @ViewInject(id = R.id.goods_price_tv)
    private TextView goods_price_tv;

    @ViewInject(id = R.id.select_goods_params_tv)
    private TextView select_goods_params_tv;


    @ViewInject(id = R.id.goods_params)
    private LinearLayout goods_params;


    @ViewInject(id = R.id.add_cart_tv, click = "add_cart_tv_click")
    private TextView add_cart_tv;

    private List<JsonMap<String, Object>> colorJsonMap;

    private String goodsId;
    private String goodsName;
    private String goodsPrice;
    /**
     * 当前展示的商品的可选参数的列表信息
     */
    private List<TagView> pips;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_goods_params_layout);
        Intent intent = getIntent();

        String colorJson = intent.getStringExtra(Constant.COLOR_JSON_STR);
        colorJsonMap = JsonParseHelper.getList_JsonMap(colorJson);
        goodsId = intent.getStringExtra(Constant.ID);
        goodsName = intent.getStringExtra(Constant.NAME);
        goodsPrice = intent.getStringExtra(Constant.GOODS_PRICE);
//		goodsSizeAdapter = new ExpandChildAdapter(this);
//		goodsColorAdapter = new ExpandChildAdapter(this);
        setParamsView();
        select_goods_params_tv.setText(getProductParamsSelectTitle());
        goods_price_tv.setText(StringUtil.getFormatMoneyWithSign(goodsPrice));
        int IsSelectStock = intent.getIntExtra(ExtraKeys.Key_Msg1, 1);
        //if(IsSelectStock>0) {
        add_cart_tv.setEnabled(true);
        add_cart_tv.setText(R.string.add2cart);

    }

    /**
     * 设置显示选项
     */
    private void setParamsView() {
        pips = new ArrayList<TagView>();
        goods_params.removeAllViews();
        for (JsonMap<String, Object> jsonMap1 : colorJsonMap) {
            View view = getLayoutInflater().inflate(R.layout.layout_product_info_params, null);
            TextView tv = (TextView) view.findViewById(R.id.l_p_i_tv_pro_param);
            tv.setText(jsonMap1.getStringNoNull("specName"));
            ArrayList<JsonMap<String, Object>> data = (ArrayList<JsonMap<String, Object>>) jsonMap1.getList_JsonMap("svList");
            TagView pipg_info = (TagView) view.findViewById(R.id.tagview);
            for (int i = 0; i < data.size(); i++) {
                JsonMap item = data.get(i);
//                Tag tag = new Tag(item.getInt("paramId"), item.getStringNoNull("paramName"));
//                tag.background = this.getResources().getDrawable(R.drawable.cus_black_white_bt);
//                tag.tagTextColor = R.drawable.goods_params_txt;
//                tag.radius = 10f;
//                if (item.getBoolean("isChecked")) {
//                    pipg_info.setSelectPosition(i);
//                }
//                pipg_info.addJustTag(tag);

            }
            pipg_info.refreshView();
//            pipg_info.setOnTagClickListener(
//                    new OnTagClickListener() {
//                        @Override
//                        public void onTagClick(View view, Tag tag, int position) {
//                            getProudctInfoByByproidandSpec(getProductParamsSelect());
//                        }
//                    }
//
//            );
            goods_params.addView(view);
            pips.add(pipg_info);
        }

    }

    /*
     * 获取商品当前选中的 商品信息
     */
//    private String getProductParamsSelect() {
//        String p = "";
//        for (TagView pa : pips) {
//            int selectIndex = pa.getSelectPosition();
//            Tag data_Select = pa.getTags().get(selectIndex);
//            p += data_Select.id + ",";
//        }
//
//        if (p.length() > 0) {
//            p = p.substring(0, p.length() - 1);
//        }
//
//        return p;
//    }

    /**
     * 获取商品当前选中的 商品信息
     */
    private String getProductParamsSelectTitle() {
        String p = "";
//        for (TagView pa : pips) {
//            int selectIndex = pa.getSelectPosition();
//            Tag data_Select = pa.getTags().get(selectIndex);
//            p += data_Select.text + ",";
//        }
//        if (p.length() > 0) {
//            p = p.substring(0, p.length() - 1);
//        }
        return p;
    }

    /**
     * 获取商品规格的数据
     */
    private void getProudctInfoByByproidandSpec(String spec) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Utils.getUserId(mContext));
        params.put("productId", goodsId);
        params.put("speStr", spec);
        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_GetProductByproidandSpec);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(GetDataConfing.What_GetProductByproidandSpec);
        getDataUtil.getData(queue);
    }


    /**
     * 加入购物车
     *
     * @param view
     */
    public void add_cart_tv_click(View view) {
        add2Cart();
    }

    private void add2Cart() {
        loadingToast.show();
        loadingToast.update(R.string.add2cart_ing);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Utils.getUserId(this));
        params.put("productId", goodsId);

//        params.put("speStr", getProductParamsSelect());//sizeId+","+colorId);
        params.put("amount", 1);
        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.GetAction_AddShoppingCart);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(GetDataConfing.What_AddShoppingCart);
        getDataUtil.getData(queue);
    }

    private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

        @Override
        public void onLoaded(GetDataQueue entity) {
            if (entity.isOk()) {
                if (ShowGetDataError.isCodeSuccess(mContext, entity.getInfo())) {
                    if (entity.what == GetDataConfing.What_AddShoppingCart) {
                        toast.showToast(R.string.add2cart_success);
                        finish();
                        MyApplication.getInstance().addShopCartNum();
                    } else if (entity.what == GetDataConfing.What_GetProductByproidandSpec) {
                        JsonMap jsonMap = JsonParseHelper.getJsonMap_List_JsonMap(entity.getInfo(), JsonKeys.Key_Info).get(0);
                        flushMsg(jsonMap);
                    }
                }
            } else {
                ShowGetDataError.showNetError(mContext);
            }
            loadingToast.dismiss();
        }

    };

    /**
     * 刷新提示信息
     *
     * @param info
     */
    private void flushMsg(JsonMap info) {
        goodsPrice = info.getStringNoNull("salePrice");
        // &amp;lt;p&amp;gt
        // tv_proAfterService.setText(Html.fromHtml(info
        // .getStringNoNull("AfterService")
        // .replace("&amp;lt;p&amp;gt;", "")
        // .replace("&amp;lt;/p&amp;gt;", "")
        // s
        // .replace("&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;",
        // "")
        // e
        // ));
        boolean has_product = info.getBoolean("isEnable");
        if (has_product) {
            add_cart_tv.setEnabled(true);
            add_cart_tv.setText(R.string.add2cart);
        } else {
            add_cart_tv.setEnabled(false);
            add_cart_tv.setText(R.string.add2cart);
            //ib_jiarugouwuche.setVisibility(View.INVISIBLE);
        }
        select_goods_params_tv.setText(getProductParamsSelectTitle());
        goods_price_tv.setText(StringUtil.getFormatMoneyWithSign(goodsPrice));
        //add_cart_tv.setEnabled(true);//test
    }

    @Override
    public void finish() {
        super.finish();
        //overridePendingTransition(R.anim.push_bottom_in, R.anim.push_bottom_out);
    }
}